package com.syt.test

import java.util.*

/**
 * 函数测试
 */

fun test(): String {
    return "hello"
}

fun sum(a: Int, b: Int): Int {
    return a + b
}

fun sum1(a: Int, b: Int) = a + b

/**
 *Unit 函数返回无意义的值：
 * 可以忽略不写
 */
fun printSum(a: Int, b: Int): Unit {
    println("sum of $a and $b = ${a + b}")
}

fun main() {
    println(test())
    println(sum(2, 2))
    println(sum1(2, -1))
    printSum(1, 11)

    // java 调用示例
    val calendar = Calendar.getInstance()
    println(calendar.get(Calendar.MONTH))

    println("请输入需要倒置的数字,不大于32位\n")
    val read = Scanner(System.`in`)
    val inputNumber = read.nextInt()

    println(System.currentTimeMillis())

    for (i in 1 .. 10) {
        println(invertNumber(inputNumber))
    }

    println(System.currentTimeMillis())

    for (i in 1 .. 10) {
        println(reverse(inputNumber))
    }

    println(System.currentTimeMillis())
}

fun invertNumber(number: Int): Int {

    var string = number.toString()
    var first: String = ""
    if (string[0] == '-') {
        first = "-"
        string = string.replaceFirst("-", "")
    }

    if (string.length == 1) {
        return string.toInt()
    }

    var str: String = ""

    for (c in string) {
        str = c + str
    }
    str = first + str

    return str.toInt()

}

fun reverse(x: Int): Int {
    var startLen = 0
    if (x < 0) {
        startLen = 1
    }
    val str = x.toString()
    if (str.length == 1) {
        return x
    }
    var lastLen = str.length - 1
    var readStr = ""
    while (lastLen >= startLen) {
        readStr += str[lastLen]
        lastLen--
    }
    return try {
        if (startLen == 0) {
            readStr.toInt()
        } else {
            readStr.toInt() * (-1)
        }
    } catch (e: NumberFormatException) {
        0
    }
}